package com.shouhuobao.take.parcel;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;
import com.shouhuobao.take.model.ParcelBean;

import java.util.ArrayList;

/**
 * 验证码取包裹列表界面
 *
 * @author coffee<br>
 * 2017年6月6日下午6:10:12
 */
public class ParcelCheckListActivity extends BaseActivity2 implements OnClickListener {

    private ListView mListView;
    private ParcelAdapter mAdapter;

    @SuppressWarnings("unchecked")
    @Override
    protected void findViewById() {
        setContentView(R.layout.take_parcel_checklist);
        super.findViewById();
        getAppTitle().setCommonTitle("取货验证");
        mListView = (ListView) findViewById(R.id.listview);
        ArrayList<ParcelBean> items = (ArrayList<ParcelBean>) getIntent().getSerializableExtra("items");
        mAdapter = new ParcelAdapter(items, getContext());
        mListView.setAdapter(mAdapter);

        findViewById(R.id.submit).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit:
                String parcelIds = getParcelIds();
                if (parcelIds != null) {
                    showLoadingDialog(null);
                    AppHttp.getInstance().takeParcelAway(parcelIds, null);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.take_parcel_away:
                cancelLoadingDialog();
                ReqResult<?> result = JSON.parser(msg.obj);
                if (checkLoginStatus(result)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Intent intent = new Intent();
                            intent.setClass(getContext(), ParcelCheckResultActivity.class);
                            startActivityForResult(intent, 102);
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return false;
            default:
                return super.handleMessage(msg);
        }
    }

    private String getParcelIds() {
        ArrayList<ParcelBean> items = mAdapter.getItems();
        if (items == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (ParcelBean item : items) {
            sb.append(item.getId());
            sb.append(",");
        }
        return sb.toString();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == 102) {
            setResult(RESULT_OK);
            finish();
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
